package Java练习.面向对象综合练习.基础;

import java.util.Scanner;

public class StudentText3 {
    public static void main(String[] args) {
        //定义数组
        Student[] arr=new Student[3];
        //创建学生对象并添加到数组
        Student stu1=new Student(1,"张三",23);
        Student stu2=new Student(2,"李四",22);
        Student stu3=new Student(3,"王五",24);
        //把学生对象添加到数组当中
        arr[0]=stu1;
        arr[1]=stu2;
        arr[2]=stu3;
        Scanner sc=new Scanner(System.in);
        System.out.print("请输入,你要查询的id:");
        int id=sc.nextInt();
        int num=select(arr,id);
        if (num==-1){
            System.out.println("该学生不存在");
        } else if (num>=0) {
            int old_age=arr[num].getAge();
            arr[num].setAge(old_age+1);
        }
        //遍历所有学生
        for (int i = 0; i < arr.length; i++) {
            Student stu=arr[i];
            if (stu!=null){
                System.out.println(stu.getId()+stu.getName()+stu.getAge());
            }
        }

    }
    public static int select(Student[] num,int id){
        for (int i = 0; i < num.length; i++) {
            //判断每个学生对象都存在数组当中,排除不存在的索引位(数组中已经存在的学生id),防止报错
            Student stu=num[i];
            if(stu!=null){
                if(id==stu.getId()){
                    return id;
                }
            }
        }
        return -1;//  Java中不存在-1索引.
    }
}